<?php
namespace app\backend\controller;

use app\backend\model\People as PeopleModel;
use app\backend\model\Duty as DutyModel;
use think\Db;
use think\Request;
use app\backend\org\Image;

class Duty extends Base
{
    public function index($id){
        $model = new DutyModel;
        // 获取分页显示
        $result = $model->getAll($id)->paginate(5);
        $page = $result->render();

        $this->assign('page',$page);
        $this->assign('duty',$result);
        $id = Request::instance()->param("id");
        $this->assign('id',$id);
        $this->assign('empty',"暂无数据");
        return $this->fetch();
    }
    //新增职称
    public function duty_create($id){
        if (Request::instance()->isPost())
        {
            $model = new DutyModel;
            $post = Request::instance()->post();// 获取经过过滤的全部post变量
            //var_dump($post);exit;
            $post["people_id"] = $id;
            //手动排序
            $maual_sort = Db::query("select sort from ls_duty where people_id='".$post["people_id"]."' and  delete_time is null");
            //sort函数 排序 [0]最小值
            if(!empty($maual_sort)){
                sort($maual_sort);
                //最大值
                $max = $maual_sort[count($maual_sort)-1];
                $post["sort"]= $max["sort"]+1;
            }else{
                $post["sort"]= 1;
            }

            //var_dump( $post["sort"]);exit;
                //插入intro数据表
                $duty_con = $this->validate($post,'Duty.edit');
                if(true !== $duty_con){
                    // 验证失败 输出错误信息
                    dump($duty_con);
                    return $this->error($msg = '参数错误，验证失败');
                }
                // 登录人的ID
                $post["creator_id"] = \think\Session::get('login_id');
                //$model->insert($post);
                // 过滤掉OTHER表中没有的字段，保存
                $line = $model->allowField(true)->save($post);
            if(!$line){
                return $this->error($this->update_error);
            } else {
                return $this->redirect('duty/index',["id"=>$id]);
            }
        }

        $people_name = PeopleModel::where(["people_id"=>$id,"delete_time"=>["exp","is null"]])->value("name_cn");
        $this->assign('name',$people_name);
        $id = Request::instance()->param("id");
        $this->assign('id',$id);

        return $this->fetch();
    }

    public function duty_update($duty_id){
        $model = new DutyModel;
        $id = $model->where(["duty_id"=>$duty_id,"delete_time"=>["exp","is null"]])->value("people_id");
        if (Request::instance()->isPost())
        {
            $model = new DutyModel;
            $post = Request::instance()->post();// 获取经过过滤的全部post变量
//var_dump($post);exit;
            //验证表单并插入数据库
            $result = $this->validate($post,'Duty.edit');
            if(!$result){
                $this->error($result->getError());
            }
            // 过滤掉数据表中没有的字段，保存
            $result = $model->allowField(true)->save($post,['duty_id'=>$duty_id]);
            //echo $result;exit;
            if(false === $result){
                $this->error($this->update_error);
            } else {
                $this->success('修改成功',url('duty/index',["id"=>$id]));
            }
        }

        //根据主键插记录
        $record = DutyModel::get($duty_id);
        //var_dump($record);exit;
        $this->assign('duty_id',$duty_id);
        $this->assign('id',$id);
        $this->assign('duty', $record);
        $people_name = PeopleModel::where(["people_id"=>$id,"delete_time"=>["exp","is null"]])->value("name_cn");
        $this->assign('name',$people_name);
        return $this->fetch();
    }

    //删除简介
    public function duty_delete($duty_id){
        $id = DutyModel::where(["duty_id"=>$duty_id,"delete_time"=>["exp","is null"]])->value("people_id");
        DutyModel::destroy($duty_id);
        $this->redirect(url('duty/index',["id"=>$id]));
    }




}